package org.myself.meeting.service;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.myself.meeting.mapper.FormeDao;
import org.myself.meeting.model.Employee;
import org.myself.meeting.model.Formepublish;
import org.myself.meeting.model.Fromeback;
import org.myself.meeting.model.Fromebackinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杜宇康
 * @create 2021—03-21 19:05
 */
@Service
public class FormeService {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    FormeDao formeDao;

    public int addForme(Formepublish forme){
        SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdate.format(new Date());
        forme.setPultime(new BigInteger(format));
        forme.setPublishid(new BigInteger(format+(""+forme.getEmployeeid())));
        return formeDao.addForme(forme);

    }

    public int deleteForme(BigInteger publishid) {
        return formeDao.deleteForme(publishid);
    }

    public int updateForme(Formepublish forme) {
        return formeDao.updateForme(forme);
    }

    public List<Formepublish> selectAll(int page) {
        page = (page-1)*10;
        return formeDao.selectAll(page);
    }

    public List<List<Fromebackinfo>> selectAlldis(BigInteger publishid, int page) {
        List<List<Fromebackinfo>> lists = new ArrayList<List<Fromebackinfo>>();
        page = (page-1)*5;
        List<Fromebackinfo> fromebacks = formeDao.selectAlldis(publishid, page);

        for (Fromebackinfo back:fromebacks) {
            List<Fromebackinfo> list = new ArrayList<Fromebackinfo>();
            list.add(back);
            BigInteger discussid = back.getDiscussid();
            List<Fromebackinfo> list1 = formeDao.selectdis(back.getDiscussid());
            getselectdis(list, list1);
            lists.add(list);
        }
        return lists;
    }


    public int addFormedis(Fromeback back) {
        Employee emp =employeeService.getEmpsByUsername(back.getUsername()) ;
        SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdate.format(new Date());
        back.setBacktime(new BigInteger(format));
        back.setDiscussid(new BigInteger(format+emp.getEmployeeid()));
        return formeDao.addFromedis(back);
    }

    public int deleteFormedis(BigInteger discussid) {
        return formeDao.deleteFormedis(discussid);
    }

    public int addFormedisac(Fromeback back) {
        Employee emp =employeeService.getEmpsByUsername(back.getUsername()) ;
        SimpleDateFormat sdate = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdate.format(new Date());
        back.setBacktime(new BigInteger(format));
        back.setDiscussid(new BigInteger(format+emp.getEmployeeid()));
        return formeDao.addFormedisac(back);
    }

    public void getselectdis(List<Fromebackinfo> list, List<Fromebackinfo> list1){
//        int i = 1;
//        System.out.println("a"+i);

//        while(list1 != null&&list1.size()!=0){
//            System.out.println("b"+i++);
            for (Fromebackinfo info:list1) {
//                System.out.println("c"+i++);
                list.add(info);
                list1 = formeDao.selectdis(info.getDiscussid());
                getselectdis(list,list1);
            }
//        }
//        System.out.println("d"+i++);
//        return;
    }

    public Integer selectcount() {
        return formeDao.selectcount();
    }

    public Integer selectcondis(BigInteger publishid){
        return formeDao.selectcondis(publishid);
    }

}
